package com.china08.yxyapi.api.schwebsite.cookbook;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.mg.cookbook.CookBook;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.cookbook.YxySchManageCookbookRepository;
import com.china08.yxyapi.util.ThumborUtil;

@CrossOrigin
@RestController
public class SwsCookBookGetController extends YxyApiBaseController {

    @Autowired
    private YxySchManageCookbookRepository yxySchManageCookbookRepository;

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @RequestMapping(value = "/api_yw/schwebsite/cookbook/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String domainName = getDomainName(request);
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains byDomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != byDomainName, "域名对象为空");

        String schoolId = byDomainName.getSchoolId();
        // 周日为 1 其他+1
        if (reqModel.week == 7) {
            reqModel.week = 1;
        } else {
            reqModel.week = reqModel.week + 1;
        }
        // schoolId + week
        String byId = schoolId + reqModel.week;
        CookBook cookBook = yxySchManageCookbookRepository.findById(byId);
        System.out.println("=-==========");
        RespModel model = new RespModel();
        if (cookBook != null) {
            model.breakfast = new CookBookModel(cookBook.getBreakfast().getName(), cookBook.getBreakfast().getImages());
            model.breakfastCakes = new CookBookModel(cookBook.getBreakfastCakes().getName(),
                    cookBook.getBreakfastCakes().getImages());
            model.lunch = new CookBookModel(cookBook.getLunch().getName(), cookBook.getLunch().getImages());
            model.lunchCakes = new CookBookModel(cookBook.getLunchCakes().getName(),
                    cookBook.getLunchCakes().getImages());
            model.dinner = new CookBookModel(cookBook.getDinner().getName(), cookBook.getDinner().getImages());
            model.status = 1;
        } else {
            model.status = 0;
            model.breakfast = new CookBookModel();
            model.breakfastCakes = new CookBookModel();
            model.lunch = new CookBookModel();
            model.lunchCakes = new CookBookModel();
            model.dinner = new CookBookModel();
        }
        return model;
    }

    protected static class ReqModel {
        // 星期 1 - 7
        public Integer week;
    }

    protected static class RespModel {
        // 早餐
        public CookBookModel breakfast;
        // 早点
        public CookBookModel breakfastCakes;
        // 午餐
        public CookBookModel lunch;
        // 午餐点心
        public CookBookModel lunchCakes;
        // 晚餐
        public CookBookModel dinner;
        // 0 无 1有
        public Integer status = 1;

    }

    protected static class CookBookModel {
        public CookBookModel(String name, List<String> images) {
            List<String> imgChuli = new ArrayList<String>();
            for (String string : images) {
                imgChuli.add(ThumborUtil.schoolWebSiteResize(string, 140, 90));
            }
            this.name = name;
            this.images = imgChuli;
        }

        public CookBookModel() {
            this.name = "";
            this.images = new ArrayList<>();
        }

        public String name;
        public List<String> images;
    }

}
